﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workbench.Shared;

namespace Workbench.Presentation
{
    public class UserSettings
    {
        public class MostRecentlyUsedProjects
        {
            public static string[] GetProjects()
            {
                string[] projectDbPaths = GetValue("MostRecentlyUsedProjects") as string[];
                return projectDbPaths;
            }
            public static void AddProject(string projectDbPath)
            {
                string[] projectDbPaths = GetValue("MostRecentlyUsedProjects") as string[] ?? new string[0];
                var result = new List<string>() { projectDbPath };
                for (int i = 0; i < projectDbPaths.Length; i++)
                {
                    if (string.Compare(projectDbPath, projectDbPaths[i], true) == 0)
                    {
                        continue;
                    }
                    result.Add(projectDbPaths[i]);
                    if (result.Count >= 10)
                    {
                        break;
                    }
                }
                SetValue("MostRecentlyUsedProjects", result.ToArray(), RegistryValueKind.MultiString);
            }

            public static void RemoveProject(string projectDbPath)
            {
                string[] projectDbPaths = GetValue("MostRecentlyUsedProjects") as string[] ?? new string[0];
                var remaining = projectDbPaths.Where(x => string.Compare(x, projectDbPath, true) != 0).ToArray();
                SetValue("MostRecentlyUsedProjects", remaining, RegistryValueKind.MultiString);
            }
        }

        private static string lastUsedDirectory;
        public static string LastUsedDirectory 
        { 
            get
            {
                if (string.IsNullOrEmpty(lastUsedDirectory))
                {
                    lastUsedDirectory = GetValue("LastUsedDirectory") as string;
                    if(lastUsedDirectory == null)
                    {
                        LastUsedDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    }
                }
                return lastUsedDirectory;
            }
            set
            {
                lastUsedDirectory = value;
                SetValue("LastUsedDirectory", lastUsedDirectory);
            }
        }

        private static int windowWidth = -1;
        public static int WindowWidth
        {
            get
            {
                if (windowWidth == -1)
                {
                    try
                    {
                        windowWidth = (int)GetValue("WindowWidth");
                    }
                    catch
                    {
                        windowWidth = 740;
                    }
                }
                return windowWidth;
            }
            set
            {
                windowWidth = value;
                SetValue("WindowWidth", windowWidth, RegistryValueKind.DWord);
            }
        }

        private static int windowHeight = -1;
        public static int WindowHeight
        {
            get
            {
                if (windowHeight == -1)
                {
                    try
                    {
                        windowHeight = (int)GetValue("WindowHeight");
                    }
                    catch
                    {
                        windowHeight = 600;
                    }
                }
                return windowHeight;
            }
            set
            {
                windowHeight = value;
                SetValue("WindowHeight", windowHeight, RegistryValueKind.DWord);
            }
        }

        private static RegistryKey GetRootKey()
        {
            return Registry.CurrentUser.CreateSubKey(@"Software\Workbench\", RegistryKeyPermissionCheck.ReadWriteSubTree);
        }
        private static void SetValue(string key, object value, RegistryValueKind kind = RegistryValueKind.String)
        {
            GetRootKey().SetValue(key, value, kind);
        }
        private static object GetValue(string key)
        {
            var rootKey = GetRootKey();

            var names = rootKey.GetValueNames();
            if(!names.Contains(key)) {
                return null;
            }
            var value = rootKey.GetValue(key);
            return value;
        }

    } 
}
